package com.lg.concurrent.aqs_cas.mashibing.s5;

import java.util.concurrent.CountDownLatch;

public class Demo5 {
    public static int sum = 0;

    //优雅的同步线程
    public static CountDownLatch countDownLatch = new CountDownLatch(100);
    public static LgLock lgLock = new LgLock();
    public static void main(String[] args) throws InterruptedException {

        System.out.println("当前核数： " + Runtime.getRuntime().availableProcessors());
        Thread[] threads = new Thread[100];
        for (int i = 0; i < threads.length; i++) {
            threads[i] = new Thread(() -> {
                try {
                    lgLock.lock();
                    for (int j = 0; j < 100; j++)
                        sum++;
                }finally {
                    lgLock.unlock();
                    countDownLatch.countDown();
                }
            });
        }

        for (Thread thread : threads) {
            thread.start();
        }

        countDownLatch.await();

        System.out.println(sum);

    }
}
